<!-- 图片 -->
<template>
    <div class="picture" :style="boxStyle">
        <el-image :src="element.eles.content" fit="fill">
            <template #placeholder>
                <div class="picture-image-slot">
                    图片加载中...
                </div>
            </template>
            <template #error>
                <div :class="{ 'no-image': !backgroundColor }" class="picture-image-slot">
                    {{ !backgroundColor && !element.eles.content ? '请添加图片' : '图片错误' }}
                </div>
            </template>
        </el-image>
    </div>
</template>

<script>
import { defineComponent, toRefs, computed } from 'vue'
export default defineComponent({
    name: 'WidgetPicture',
    props: {
        element: {
            type: Object,
            default: () => ({})
        }
    },
    setup(props, { emit }) {
        const { element } = toRefs(props)

        const boxStyle = computed(() => {
            const style = element.value.eles.style || {}
            return {
                width: style.width,
                height: style.height,
                opacity: style.opacity,
                marginTop: style['margin-top'],
                marginLeft: style['margin-left'],
                marginRight: style['margin-right'],
                borderStyle: style['border-style'],
                borderColor: style['border-color'],
                borderWidth: style['border-width'],
                borderRadius: style['border-radius'],
                backgroundColor: style['background-color'],
                boxShadow: style['box-shadow']
            }
        })
        const backgroundColor = computed(() => element.value.eles.style['background-color'])
        
        return {
            element,
            boxStyle,
            backgroundColor
        }
    }
})
</script>

<style lang="less" scoped>
@import './widget-picture.less';
</style>